package com.android.clinic.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.clinic.app.R;
import com.android.clinic.app.entity.DoctorInfo;
import com.android.clinic.app.glide.ImageLoaderUtils;
import com.android.clinic.app.ui.doctor.AskQuestionWithDoctorActivity;
import com.android.clinic.app.ui.doctor.DoctorDetailActivity;
import com.android.clinic.app.ui.widget.UIAlertView;
import com.android.clinic.app.utils.Constants;
import com.android.clinic.app.utils.LocalDataUtils;
import com.android.clinic.app.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/1/26.
 */
public class HomeRecommendAdapter extends BaseDelegateAdapter {

    private Activity activity;

    private List<DoctorInfo> lists = new ArrayList<>();

    public HomeRecommendAdapter(Context context, int... resource) {
        super(context, R.layout.item_home_recommend);
        activity = (Activity) context;
    }

    @Override
    protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
        return new ViewHolder(convertView);
    }

    @Override
    protected void setupBindViewHolder(Object viewHolder, int position, int typeIndex) {
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.tvName.setText(lists.get(position).getName());
        holder.tvPosition.setText(lists.get(position).getPosition());
        holder.tvHospital.setText(lists.get(position).getHospital());
        holder.tvReservation.setText(String.valueOf((int)(Math.random()*1000)));
        holder.tvEvaluation.setText(String.valueOf((int)(Math.random()*100)));
        holder.tvMotto.setText(lists.get(position).getMotto());
        holder.tvDeptName.setText(lists.get(position).getDeptName());

        if (!Utils.isEmpty(lists.get(position).getImgUrl()) && lists.get(position).getImgUrl().contains(".jpg")) {
            holder.ivHead.setVisibility(View.VISIBLE);
            holder.layDefault.setVisibility(View.GONE);
            ImageLoaderUtils.getInstance().loadHeadImage(lists.get(position).getImgUrl(), holder.ivHead);
        } else {
            holder.ivHead.setVisibility(View.GONE);
            holder.layDefault.setVisibility(View.VISIBLE);
            holder.tvDefaultName.setText(lists.get(position).getName());
        }

        holder.convertView.setOnClickListener(v -> {
            if (!Utils.isFastClick()) {
                String doctorId = LocalDataUtils.getData(Constants.QUESTION_ID);
                // 如果doctorId不为null，说明该用户没有对医生进行提问
                if (!Utils.isEmpty(doctorId)) {
                    UIAlertView.alertDialog(activity, "操作提示",
                            "您还有一个提问待确认?", new UIAlertView.OnThemeDialogListener() {
                                @Override
                                public void OnDismissButton() {
                                    // 用户放弃提问，清除缓存，跳转到医生详情
                                    LocalDataUtils.removeData(Constants.QUESTION_ID);
                                    Intent intent = new Intent(activity, DoctorDetailActivity.class);
                                    intent.putExtra("doctorId", lists.get(position).getId());
                                    intent.putExtra("reservation", holder.tvReservation.getText().toString());
                                    intent.putExtra("evaluation", holder.tvEvaluation.getText().toString());
                                    activity.startActivity(intent);
                                }
                                @Override
                                public void OnSureButton() {
                                    // 去提问页面，继续提问
                                    Intent intent = new Intent(activity, AskQuestionWithDoctorActivity.class);
                                    intent.putExtra("doctorId", doctorId);
                                    activity.startActivity(intent);
                                }
                            },"继续提问","放弃提问");

                } else {
                    Intent intent = new Intent(activity, DoctorDetailActivity.class);
                    intent.putExtra("doctorId", lists.get(position).getId());
                    intent.putExtra("reservation", holder.tvReservation.getText().toString());
                    intent.putExtra("evaluation", holder.tvEvaluation.getText().toString());
                    activity.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (lists != null && lists.size() > 0) {
            return lists.size();
        } else {
            return 0;
        }
    }

    public void setRecommendList(List<DoctorInfo> recommendList) {
        if (recommendList != null && recommendList.size() > 0) {
            this.lists = recommendList;
            notifyDataSetChanged();
        }
    }


    private class ViewHolder extends BaseViewHolder {

        ImageView ivHead;
        TextView tvName, tvPosition, tvHospital, tvDefaultName,
                tvReservation, tvEvaluation, tvMotto, tvDeptName;
        View convertView;
        FrameLayout layDefault;

        public ViewHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvPosition = (TextView) itemView.findViewById(R.id.tv_position);
            tvHospital = (TextView) itemView.findViewById(R.id.tv_hospital);
            tvDefaultName = (TextView) itemView.findViewById(R.id.tv_default_name);
            tvReservation = (TextView) itemView.findViewById(R.id.tv_reservation);
            tvEvaluation = (TextView) itemView.findViewById(R.id.tv_evaluation);
            layDefault = (FrameLayout) itemView.findViewById(R.id.layout_default);
            tvMotto = (TextView) itemView.findViewById(R.id.tv_motto);
            tvDeptName = (TextView) itemView.findViewById(R.id.tv_dept_name);
        }
    }

}
